package com.jjwxc.jwjskandriod.activity;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.adapter.MyPurchaseAdapter;
import com.jjwxc.jwjskandriod.config.BaseActivity;

public class MyPurchaseActivity extends BaseActivity {

    /**
     * 菜单标题
     */
    private final String[] TAB_TITLES = new String[]{"书籍", "包月", "打赏"};
    ViewPager viewPager;
    TabLayout tabLayout;
    /**
     * 页卡适配器
     */
    private PagerAdapter adapter;

    @Override
    public int getContentViewId() {
        return R.layout.activity_purchase;
    }

    @Override
    public void findView() {
        viewPager = findViewById(R.id.view_purchase_pager);
        tabLayout = findViewById(R.id.tab_purchase_layout);
    }

    @Override
    public void setListener() {
        setTitle("我的购买");
    }

    @Override
    public void afterCreate() {
        setTabs(tabLayout, getLayoutInflater(), TAB_TITLES);
        initPager();
    }


    private void initPager() {
        adapter = new MyPurchaseAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);

        // 关联切换
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                // 取消平滑切换
                viewPager.setCurrentItem(tab.getPosition(), true);
//                if (tab.getPosition() == 0) {
//                    ChangeStatusBarTextColor.changeColor(getActivity(), false, "#FFFFFF");
//                } else if (tab.getPosition() == 1) {
//                    ChangeStatusBarTextColor.changeColor(getActivity(), true, "#FFFFFF");
//                }

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    /**
     * 设置页卡显示效果
     *
     * @param tabLayout
     * @param inflater
     * @param tabTitlees
     */
    private void setTabs(TabLayout tabLayout, LayoutInflater inflater, String[] tabTitlees) {
        for (int i = 0; i < tabTitlees.length; i++) {
            TabLayout.Tab tab = tabLayout.newTab();
            View view = inflater.inflate(R.layout.bookshelf_item_menu, null);
            // 使用自定义视图，目的是为了便于修改，也可使用自带的视图
            tab.setCustomView(view);
            TextView tvTitle = (TextView) view.findViewById(R.id.txt_bookshelf_tab);
            tvTitle.setText(tabTitlees[i]);
            tabLayout.addTab(tab);
        }
    }
}
